//
//  CommentListViewModel.m
//  CommentPage
//
//  Created by 欧杨 on 2020/11/29.
//

#import "CommentListViewModel.h"
#import "CommentCellViewModel.h"
#import "CommentModel.h"
#import "UserModel.h"
#import "Comment.h"
#import "Reply.h"
#import <AFHTTPSessionManager.h>
#define testContentID @"5fc301a3f5beb29bc3968b54"
#define testUserID @"5fbde0e0f5beb22628d4b697"

@interface CommentListViewModel()

@property(assign, nonatomic) bool isReady;

@end


@implementation CommentListViewModel

- (instancetype)initWithContentID:(NSString *)contentID andUserID:(NSString *)userID
{
    self = [super init];
    if (self) {
        self.contentID = contentID;
        self.userID = userID;
        self.isReady = NO;
    }
    return self;
}

- (void)getLikeListWithComments
{
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/like"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.likedList = [NSMutableArray array];
        if (![responseObject[@"Data"] isKindOfClass:[NSNull class]]) {
            for (NSString *likedCommentID in responseObject[@"Data"]) {
                [self.likedList addObject:likedCommentID];
            }
        }
        [self getComments];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed");
    }];
}

- (void)getComments
{
    self.isReady = NO;
    NSString *URLString = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/comment/", self.contentID];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            self.comments = [NSMutableArray array];
        if (![responseObject[@"Data"] isKindOfClass:[NSNull class]]) {
            for (NSDictionary *dic in responseObject[@"Data"]) {
            CommentModel *comment = [[CommentModel alloc] initWithDictionary:dic[@"Comment"]];
            
            NSMutableArray *replies = [NSMutableArray array];
                if (![dic[@"Replies"] isKindOfClass:[NSNull class]]) {
                    for (NSDictionary *replyDic in dic[@"Replies"]) {
                        CommentModel *content = [[CommentModel alloc] initWithDictionary:replyDic[@"Reply"]];
                        UserModel *user = [[UserModel alloc] initWithDictionary:replyDic[@"User"]];
                        UserModel *father = [[UserModel alloc] initWithDictionary:replyDic[@"Father"]];
                        Reply *reply = [[Reply alloc] initWithReply:content Father:father User:user];
                        [replies addObject:reply];
                    }
                }
            
            UserModel *user = [[UserModel alloc]
                               initWithDictionary:dic[@"User"]];
            
            CommentCellViewModel *oneComment = [[CommentCellViewModel alloc] initWithComment:[[Comment alloc]initWithComment:comment Replies:replies User:user]];
                NSLog(@"%@",self.likedList);
                if ([self.likedList containsObject:comment.ID]) {
                    NSLog(@"已点赞");
                    oneComment.isLike = YES;
                }
                else {
                    NSLog(@"未点赞");
                    oneComment.isLike = NO;
                }
                
            [self.comments addObject:oneComment];
        }
        }
        self.isReady = YES;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed");
    }];
}

- (void)addNewComment:(NSString *)content
{
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/comment"];
    NSDictionary *postParam = @{@"contentId":self.contentID,
                                @"fatherId":self.userID,
                                @"content":content,
                                @"isReply":@NO};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:URLString parameters:postParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"success");
        [self getLikeListWithComments];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

@end
